package com.video.web.controller.system;

import java.util.List;

import com.video.common.utils.MinioAPI;
import com.video.system.domain.VideoTeacherTitle;
import com.video.system.service.IVideoTeacherTitleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.video.common.annotation.Log;
import com.video.common.enums.BusinessType;
import com.video.system.domain.VideoTeacher;
import com.video.system.service.IVideoTeacherService;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.utils.poi.ExcelUtil;
import com.video.common.core.page.TableDataInfo;

/**
 * 教师Controller
 * 
 * @author lyh
 * @date 2021-07-20
 */
@Controller
@RequestMapping("/system/teacher")
public class VideoTeacherController extends BaseController
{
    private String prefix = "system/teacher";

    @Autowired
    private IVideoTeacherService videoTeacherService;
    @Autowired
    private IVideoTeacherTitleService videoTeacherTitleService;
    @Autowired
    private MinioAPI minioAPI;

    /**
     * 获取上传图片的URL
     * @return
     * @throws Exception
     *
     */
    /*//获取的文件名
        VideoTeacher videoTeacher = videoTeacherService.selectAll();
        System.out.println("======================>videoTeachers："+videoTeacher);
        String objectKey = videoTeacher.getTeacherPicture();*/
    @PostMapping("/show")
    @ResponseBody
    public AjaxResult showUrl(String fileName) throws Exception {
       //桶
        String bucket="test";

        // 获取文件签名url
        String signedUrl = minioAPI.getSignedUrl(bucket, fileName, 300);
        // 该路径可访问文件五分钟
        System.out.println("=========================>signedUrl:"+signedUrl);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url", signedUrl);//存入上传路径
        return ajax;
    }

    @RequiresPermissions("system:teacher:view")
    @GetMapping()
    public String teacher()
    {
        return prefix + "/teacher";
    }

    /**
     * 查询教师列表
     */
    @RequiresPermissions("system:teacher:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VideoTeacher videoTeacher) throws Exception {
        startPage();
        List<VideoTeacher> list = videoTeacherService.selectVideoTeacherList(videoTeacher);
        return getDataTable(list);
    }

    /**
     * 导出教师列表
     */
    @RequiresPermissions("system:teacher:export")
    @Log(title = "教师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VideoTeacher videoTeacher)
    {
        List<VideoTeacher> list = videoTeacherService.selectVideoTeacherList(videoTeacher);
        ExcelUtil<VideoTeacher> util = new ExcelUtil<VideoTeacher>(VideoTeacher.class);
        return util.exportExcel(list, "教师数据");
    }

    /**
     * 新增教师
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        List<VideoTeacherTitle> videoTeacherTitles = videoTeacherTitleService.selectCodeAndNameAll();
        map.put("all",videoTeacherTitles);
        return prefix + "/add";
    }

    /**
     * 新增保存教师
     */
    @RequiresPermissions("system:teacher:add")
    @Log(title = "教师", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VideoTeacher videoTeacher)
    {
        return toAjax(videoTeacherService.insertVideoTeacher(videoTeacher));
    }

    /**
     * 修改教师
     */
    @GetMapping("/edit/{teacherId}")
    public String edit(@PathVariable("teacherId") Integer teacherId, ModelMap mmap)
    {
        VideoTeacher videoTeacher = videoTeacherService.selectVideoTeacherById(teacherId);
        mmap.put("videoTeacher", videoTeacher);

        List<VideoTeacherTitle> videoTeacherTitles = videoTeacherTitleService.selectCodeAndNameAll();
        mmap.put("all",videoTeacherTitles);

        return prefix + "/edit";
    }

    /**
     * 修改保存教师
     */
    @RequiresPermissions("system:teacher:edit")
    @Log(title = "教师", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VideoTeacher videoTeacher)
    {
        return toAjax(videoTeacherService.updateVideoTeacher(videoTeacher));
    }

    /**
     * 删除教师
     */
    @RequiresPermissions("system:teacher:remove")
    @Log(title = "教师", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(videoTeacherService.deleteVideoTeacherByIds(ids));
    }
}
